package cn.xiaopengstack.types.framework.strategy.link.mutilModel;

import java.util.Objects;

/**
 * @author jiangyangang
 */
public class BusinessLinkedList<T, D, R> extends LinkedList<ILogicLink<T, D, R>> implements ILogicLink<T, D, R> {

    public BusinessLinkedList(String name) {
        super(name);
    }

    /**
     * 从头开始遍历所有节点
     * @param request
     * @param dynamic
     * @return
     * @throws Exception
     */
    @Override
    public R apply(T request, D dynamic) throws Exception {
        Node<ILogicLink<T, D, R>> current = this.first;
        if (Objects.isNull(current)) {
            return null;
        }

        do {
            ILogicLink<T, D, R> item = current.item;
            R applyResult = item.apply(request, dynamic);
            if (Objects.nonNull(applyResult)) {
                return applyResult;
            }

            current = current.next;
        } while (current != null);


        return null;
    }
}
